package com.example.materialviewpagerdemo;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.materialviewpagerdemo.Presenter.CurrentDataController;
import com.example.materialviewpagerdemo.bean.CurrentData;
import com.example.materialviewpagerdemo.bean.ListViewModel;
import com.example.materialviewpagerdemo.bean.StaticData;
import com.example.materialviewpagerdemo.view.OnItemClickListener;
import com.example.materialviewpagerdemo.view.RecyclerViewHolder;
import com.github.florent37.materialviewpager.header.MaterialViewPagerHeaderDecorator;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RecyclerViewFragment extends Fragment {
    public static Fragment newInstance(){return  new RecyclerViewFragment();}
    final List<ListViewModel> items = new ArrayList<>();
    static final int ITEMS = 6;
    ListViewModel[] listViewModels=new ListViewModel[13];

    private ZLoadingDialog dialog;
    RecyclerView mRecyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recyclerview, container, false);
    }
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView=view.findViewById(R.id.recyclerView);
        dialog = new ZLoadingDialog(RecyclerViewFragment.this.getActivity());

        for (int i=0;i<13;i++) {
            listViewModels[i]=new ListViewModel();
        }
        //
        listViewModels[0].setImgId(R.drawable.ic_fengsu);
        listViewModels[0].setDataName("风速");
        //
        listViewModels[1].setImgId(R.drawable.ic_fengxiang);
        listViewModels[1].setDataName("风向");
        //
        listViewModels[2].setImgId(R.drawable.ic_sunshine);
        listViewModels[2].setDataName("光照强度");
        //
        listViewModels[3].setImgId(R.drawable.ic_airtem);
        listViewModels[3].setDataName("空气温度1");
        //
        listViewModels[4].setImgId(R.drawable.ic_airhum);
        listViewModels[4].setDataName("空气湿度1");
        //
        listViewModels[5].setImgId(R.drawable.ic_soilhum);
        listViewModels[5].setDataName("土壤湿度1");
        //
        listViewModels[6].setImgId(R.drawable.ic_soilsalt);
        listViewModels[6].setDataName("土壤盐分1");
        //
        listViewModels[7].setImgId(R.drawable.ic_soiltem);
        listViewModels[7].setDataName("土壤温度1");
        //
        listViewModels[8].setImgId(R.drawable.ic_airtem);
        listViewModels[8].setDataName("空气温度2");
        //
        listViewModels[9].setImgId(R.drawable.ic_airhum);
        listViewModels[9].setDataName("空气湿度2");
        //
        listViewModels[10].setImgId(R.drawable.ic_soilhum);
        listViewModels[10].setDataName("土壤湿度2");
        //
        listViewModels[11].setImgId(R.drawable.ic_soilsalt);
        listViewModels[11].setDataName("土壤盐分2");
        //
        listViewModels[12].setImgId(R.drawable.ic_soiltem);
        listViewModels[12].setDataName("土壤温度2");
        for (ListViewModel listViewModel:listViewModels) {
            items.add(listViewModel);
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new MaterialViewPagerHeaderDecorator());
        RecyclerViewPagerAdapter recyclerViewPagerAdapter=new RecyclerViewPagerAdapter(this.getActivity(),items);
        recyclerViewPagerAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                Intent intent=new Intent(RecyclerViewFragment.this.getActivity(),ChartActivity.class);
                intent.putExtra("id",postion);
                startActivity(intent);
            }
        });
        mRecyclerView.setAdapter(recyclerViewPagerAdapter);
        dialog.setLoadingBuilder(Z_TYPE.STAR_LOADING)//设置类型
                .setLoadingColor(getResources().getColor(R.color.accent_color))//颜色
                .setHintText("正在加载...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(getResources().getColor(R.color.accent_color))  // 设置字体颜色
                .setDurationTime(1) // 设置动画时间百分比 - 0.5倍
                .setDialogBackgroundColor(getResources().getColor(R.color.colorWhite)) // 设置背景色，默认白色
                .show();
        getCurrentData();

    }
    private void getCurrentData(){
        Retrofit retrofit=new Retrofit.Builder().baseUrl(StaticData.url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CurrentDataController currentDataController=retrofit.create(CurrentDataController.class);
        Call<CurrentData> currentDataCall=currentDataController.queryOne();
        currentDataCall.enqueue(new Callback<CurrentData>() {
            @Override
            public void onResponse(Call<CurrentData> call, Response<CurrentData> response) {

                try {
                    listViewModels[0].setDataContent(response.body().getFengsu() + "m/s");
                    listViewModels[1].setDataContent(toChinese(response.body().getFengxiang()));
                    listViewModels[2].setDataContent(response.body().getSunLight() + "kLux");
                    listViewModels[3].setDataContent(response.body().getAirTemp1() + "℃");
                    listViewModels[4].setDataContent(response.body().getAirHumi1() + "%");
                    listViewModels[5].setDataContent(response.body().getSoilHumi1() + "%");
                    listViewModels[6].setDataContent(response.body().getSoilSalt1() + "dS/m");
                    listViewModels[7].setDataContent(response.body().getSoilTemp1() + "℃");
                    listViewModels[8].setDataContent(response.body().getAirTemp2() + "℃");
                    listViewModels[9].setDataContent(response.body().getAirHumi2() + "%");
                    listViewModels[10].setDataContent(response.body().getSoilHumi2() + "%");
                    listViewModels[11].setDataContent(response.body().getSoilSalt2() + "dS/m");
                    listViewModels[12].setDataContent(response.body().getSoilTemp2() + "℃");
                }catch (NullPointerException e){
                    for (int i=0;i<13;i++){
                        listViewModels[i].setDataContent("");
                    }
                    Toast.makeText(RecyclerViewFragment.this.getActivity(),"数据获取失败,请检查网络后重试",Toast.LENGTH_SHORT).show();
                }
                RecyclerViewPagerAdapter recyclerViewPagerAdapter=new RecyclerViewPagerAdapter(RecyclerViewFragment.this.getActivity(),items);
                recyclerViewPagerAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int postion) {
                        Intent intent=new Intent(RecyclerViewFragment.this.getActivity(),ChartActivity.class);
                        intent.putExtra("id",postion);
                        startActivity(intent);
                    }
                });
                mRecyclerView.setAdapter(recyclerViewPagerAdapter);
                dialog.cancel();
            }

            @Override
            public void onFailure(Call<CurrentData> call, Throwable t) {
                dialog.cancel();
                System.out.println("连接超时");
            }
        });
    }




    private String toChinese(String stringAngle){
        double angle=Double.parseDouble(stringAngle);
        if (angle>=0&&angle<22.5)
            return "北风";
        if (angle>=22.5&&angle<67.5)
            return "东北风";
        if (angle>=67.5&&angle<112.5)
            return "东风";
        if (angle>=112.5&&angle<157.5)
            return "东南风";
        if (angle>=157.5&&angle<202.5)
            return "南风";
        if (angle>=202.5&&angle<247.5)
            return "西南风";
        if (angle>=247.5&&angle<292.5)
            return "西风";
        if (angle>=292.5&&angle<337.5)
            return "西北风";
        if (angle>=337.5&&angle<=360)
            return "北风";
        return "参数错误";
    }
}
